home *** CD-ROM | disk | FTP | other *** search
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
-
- void xstdlib();
-
- int roughly(double x, double y)
- {
- if(y==0.0)
- return (fabs(x) < 1.0e-6);
- else
- return (fabs((y-x)/y) < 1.0e-6);
- }
-
- int main()
- {
- xstdlib();
- }
-
- void xstdlib()
- {
- int i,j;
- char *p, buf[50];
-
- printf("==> starting xstdlib <==\n");
-
- // --- first we test the functions defined in strtol.c
- assert(strtol("0xface",&p,0)==0xfaceL && *p=='\0');
- assert(strtol("+f0adg",&p,16)==0xf0adL && *p=='g');
- assert(strtol("-caca0d",&p,13)==-366860L && *p=='d');
-
- assert(strtoul("0xface",&p,0)==0xfaceL && *p=='\0');
- assert(strtoul("+f0adg",&p,16)==0xf0adL && *p=='g');
- assert(strtoul("-caca0d",&p,13)==0L && *p=='-');
-
- assert(atoi("314A")==314 && atoi("-431")==-431);
- assert(atol("0234567")==234567L && atol("-98765")==-98765L);
-
- assert(abs(10)==10 && abs(-10)==10);
- assert(labs(102030L)==102030L && labs(-102030L)==102030L);
- printf("passed strtol/strtoul/atoi/atol/abs/labs... ");
-
- // --- test itoa and ltoa
- assert(itoa(-3456,buf,10)==buf && strcmp(buf,"-3456")==0);
- assert(itoa(12345,buf,10)==buf && strcmp(buf,"12345")==0);
-
- assert(itoa(065172,buf,8)==buf && strcmp(buf,"65172")==0);
- assert(itoa(0xface,buf,16)==buf && strcmp(buf,"face")==0);
- assert(itoa(-12345,buf,16)==buf && strcmp(buf,"cfc7")==0);
-
- assert(ltoa(123456L,buf,10)==buf && strcmp(buf,"123456")==0);
- assert(ltoa(-213456L,buf,10)==buf && strcmp(buf,"-213456")==0);
-
- assert(ltoa(06532174L,buf,8)==buf && strcmp(buf,"6532174")==0);
- assert(ltoa(0xfaeced23L,buf,16)==buf && strcmp(buf,"faeced23")==0);
- assert(ltoa(0xfaeced23L,buf,10)==buf && strcmp(buf,"-85136093")==0);
- printf("passed itoa/ltoa...\n");
-
- // --- test strtod and atof
- assert(roughly(strtod("469.0123zap",&p),469.0123) && *p=='z');
- assert(roughly(strtod("000000001.23",&p),1.23) && *p=='\0');
- assert(roughly(strtod("00000000100.72",&p),100.72) && *p=='\0');
- assert(roughly(strtod("-3.1415e-123-",&p),-3.1415e-123) && *p=='-');
- assert(roughly(strtod("000001023.0052000000e+0000120A",&p),1.0230052e+123) && *p=='A');
- assert(roughly(strtod("ZX81",&p),0.0) && *p=='Z');
-
- assert(roughly(atof("2.71828"),2.71828));
- assert(roughly(atof("-4.56e-12"),-4.56e-12));
- assert(roughly(atof("0000.005600e10"),5.6e7));
- printf("passed strtod/atof... ");
-
- for(i=0; i<1000; i++)
- assert((j=rand())>=0 && j<=RAND_MAX);
- srand(3141);
- for(i=0; i<1000; i++)
- assert((j=rand())>=0 && j<=RAND_MAX);
- printf("passed rand/srand...\n");
-
- printf("passed stdlib tests...\n");
- printf("==> finished xstdlib <==\n");
-
- return;
- }
-